package model

import (
	"encoding/json"
	"ota/model/entity"
)

const (
	UpgradeV2DownLoadRatio        = 0.8
	UpgradeV2InitialProgressRatio = 0.1
	UpgradeV1InitialProgressRatio = 0.5
)

type TargetType int

const (
	TargetTypeAll TargetType = iota + 1
	TargetTypeUsers
	TargetTypeEBikeTypes
	TargetTypeEBikeIds
)

type UpgradeType int

// 发布方式，1 自主 2 强制 3 静默
const (
	OtaAutonomous UpgradeType = 1
	OtaMandatory  UpgradeType = 2
	OtaSilence    UpgradeType = 3
)
const (
	OtaAgree = iota
	OtaCancel
	OtaDelayNotify // 延迟通知,而不是延迟升级
)

type OtaUpgradeResultType int

const (
	UpgradeResultPending OtaUpgradeResultType = iota + 1
	UpgradeResultUpgrading
	UpgradeResultSuccess
	UpgradeResultFailure
	UpgradeResultAbolishment
)

func (a OtaUpgradeResultType) String() string {
	switch a {
	case UpgradeResultPending:
		return "pending"
	case UpgradeResultUpgrading:
		return "upgrading"
	case UpgradeResultSuccess:
		return "success"
	case UpgradeResultFailure:
		return "failure"
	case UpgradeResultAbolishment:
		return "abolishment"
	}
	return "unknown"
}

type OtaUpgradeResultUpgradingType int

const (
	OtaUpgradeResultUpgradingNormal OtaUpgradeResultUpgradingType = iota
	OtaUpgradeResultUpgradingQueueing
	OtaUpgradeResultUpgradingOfflineDelaying
	OtaUpgradeResultUpgradingRetrying
)

type OtaUpgradePlanStatusType int

const (
	UpgradePlanStatusPending   OtaUpgradePlanStatusType = iota + 1 // 待执行
	UpgradePlanStatusUpgrading                                     // 执行中
	UpgradePlanStatusCompleted                                     // 已完成
	UpgradePlanStatusFailed                                        // 失败
	UpgradePlanStatusCanceled                                      // 取消升级(终止)
)

func (a OtaUpgradePlanStatusType) String() string {
	switch a {
	case UpgradePlanStatusPending:
		return "pending"
	case UpgradePlanStatusUpgrading:
		return "upgrading"
	case UpgradePlanStatusCompleted:
		return "completed"
	case UpgradePlanStatusFailed:
		return "failed"
	case UpgradePlanStatusCanceled:
		return "canceled"
	}
	return "unknown"
}

type OtaFirmwareTypeOpType int

const (
	FirmwareTypeOpCreate OtaFirmwareTypeOpType = iota + 1 // 未开始
	FirmwareTypeOpUpdate                                  //
	FirmwareTypeOpFetch
	FirmwareTypeOpDelete
)

type OtaFirmwareOpType int

const (
	FirmwareOpCreate OtaFirmwareOpType = iota + 1 // 未开始
	FirmwareOpUpdate                              //
	FirmwareOpFetch
	FirmwareOpDelete
)

type OtaUpgradePlanOpType int

func (o OtaUpgradePlanOpType) String() string {

	switch o {
	case 1:
		return "create"
	case 2:
		return "update"
	case 3:
		return "delete"
	case 4:
		return "cancel"
	default:
		return "unknown"
	}
}

const (
	UpgradePlanOpCreate OtaUpgradePlanOpType = iota + 1
	UpgradePlanOpUpdate
	UpgradePlanOpDelete
	UpgradePlanOpCanceled
)

type OtaUpgradeStrategyOpType int

const (
	UpgradeStrategyOpCreate OtaUpgradeStrategyOpType = iota + 1 // 未开始
	UpgradeStrategyOpUpdate                                     //
	UpgradeStrategyOpFetch
	UpgradeStrategyOpExec
	UpgradeStrategyOpDelete
)

type OtaUserUpgradeOpType int

const (
	UserUpgradeOpCreate OtaUserUpgradeOpType = iota + 1 // 未开始
	UserUpgradeOpUpdate                                 //
	UserUpgradeOpFetch
	UserUpgradeOpExec
	UserUpgradeOpDelete
)

type UpgradeResultFrom string

const (
	UpgradeResultFromDeviceReport    UpgradeResultFrom = "device_report"
	UpgradeResultFromSelfLogic       UpgradeResultFrom = "self_logic"
	UpgradeResultFromInstructionResp UpgradeResultFrom = "ota_instruction_resp"
)

type OtaSettingRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type OtaSettingResponse struct {
}

type OtaCheckVersionRequest struct {
	SNCode string `json:"sn_code" binding:"required"`
}

type OtaCheckVersionResponse struct {
	UpgradeType   int             `json:"upgrade_type"`
	EbikeName     string          `json:"ebike_name"`
	EbikeTypeName string          `json:"ebike_type_name"`
	ID            int64           `json:"id"`
	PlanId        int64           `json:"plan_id"`
	StrategyId    int64           `json:"strategy_id"`
	Status        int             `json:"status"`
	Details       json.RawMessage `json:"details"`
}

type OtaUpgradeRequest struct {
	ID         int64  `json:"id" binding:"required"`
	SNCode     string `json:"sn_code" binding:"required"`
	Status     *int   `json:"status" binding:"required"` // 升级确认状态（0:确认,1:取消,2:延期提醒）
	PlanId     int64  `json:"plan_id" binding:"required"`
	StrategyId int64  `json:"strategy_id" binding:"required"`
}

func (a *OtaUpgradeRequest) String() string {
	b, _ := json.Marshal(a)
	return string(b)
}

type OtaUpgradePlanQueryCond struct {
	Users     []int64 `json:"users"`
	EBikeType []int64 `json:"ebike_type"`
	EbikeId   []int64 `json:"ebike_id"`
}

type OtaRecordRequest struct {
	Index  int    `json:"index"`
	Size   int    `json:"size"`
	SNCode string `json:"sn_code" binding:"required"`
}

type OtaResultRequest struct {
	Id int64 `json:"id" binding:"required"`
}

type OtaResultResponse struct {
	Status       int             `json:"status"`
	StatusDetail json.RawMessage `json:"status_detail"`
	Progress     float32         `json:"progress"`
}

type OtaRecordResponse struct {
	List  []*entity.UserUpgrade `json:"list"`
	Total int64                 `json:"total"`
}

type OtaRecordItem struct {
	Id            int64           `json:"id"`
	UserId        int64           `json:"user_id"`
	UserName      string          `json:"user_name"`
	UserPhone     string          `json:"user_phone"`
	PlanId        int64           `json:"plan_id"`
	PlanName      string          `json:"plan_name"`
	StrategyId    int64           `json:"strategy_id"`
	EbikeId       int64           `json:"ebike_id"`
	EbikeName     string          `json:"ebike_name"`
	EbikeSnCode   string          `json:"ebike_sn_code"`
	EbikeTypeId   int64           `json:"ebike_type_id"`
	EbikeTypeName string          `json:"ebike_type_name"`
	Detail        json.RawMessage `json:"detail"`
	Progress      float32         `json:"progress"`
	Status        int             `json:"status"`
	FailureReason string          `json:"failure_reason"`
	RetryCount    int             `json:"retry_count"`
	CreatedAt     int64           `json:"created_at"`
	UpdatedAt     int64           `json:"updated_at"`
}
